﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.XPath;
using System.Xml;

namespace Teamboard.Helpers
{
    public class SettingsManager
    {
        XPathNavigator _nav;

        public SettingsManager()
            : this(HttpContext.Current.Server.MapPath("Projects.xml"))
        {
        }

        public SettingsManager(string settingFile)
        {
            XmlDocument _doc = new XmlDocument();
            _doc.Load(settingFile);
            _nav = _doc.CreateNavigator();
        }

        public IEnumerable<string> GetProjectsList()
        {
            var result = new List<string>();

            XPathNodeIterator iterator = _nav.Select("projects/project/@name");
            while (iterator.MoveNext())
            {
                result.Add(iterator.Current.InnerXml);
            }

            return result;
        }

        public IEnumerable<string> GetProjectConfigurations(string projectName)
        {
            var result = new List<string>();

            XPathNodeIterator iterator = _nav.Select(string.Format("projects/project[@name = '{0}']/configurations/configuration/@name", projectName));
            while (iterator.MoveNext())
            {
                result.Add(iterator.Current.InnerXml);
            }

            return result;
        }
    }
}